/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx.dialogs;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import rpg.RPG;
import rpg.ui.components.Component;
import rpg.ui.components.ComponentState;
import rpg.ui.components.ComponentTextfield;
import rpg.ui.components.IClickableComponent;
import rpg.mouse.MouseEventType;
import rpg.ui.components.ComponentButton;

/**
 *
 * @author rothens
 */
public class DialogHandler {

    public int active;
    Dialog dialog;

    public DialogHandler(Dialog dialog) {
        this.dialog = dialog;
    }

    public boolean HandleEvents() {
        dialog.components.get(active).setState(ComponentState.SELECTED);
        MouseEvent pos = RPG.mouselistener.lastPosition;
        int mouseX = (pos == null) ? -1 : pos.getX()-dialog.x;
        int mouseY = (pos == null) ? -1 : pos.getY()-dialog.y;
        for (int i = 0; i < dialog.components.size(); i++) {
            Component c = dialog.components.get(i);
            if (c.isMouseOver(mouseX, mouseY)) {
                c.setState(ComponentState.HOVER);
            } else if (active == i) {
                c.setState(ComponentState.SELECTED);
            } else {
                c.setState(ComponentState.INACTIVE);
            }
            c.redraw();
        }
        MouseEvent e = RPG.mouselistener.mouseEvents.get(MouseEventType.CLICK);
        if (e != null) {

            for (int i = 0; i < dialog.components.size(); i++) {
                Component c = dialog.components.get(i);
                if (c instanceof IClickableComponent) {
                    if (((IClickableComponent) c).onRelease(e.getX()-dialog.x, e.getY()-dialog.y)) {
                        return true;
                    }
                } else if (c.isMouseOver(e.getX()-dialog.x, e.getY()-dialog.y)) {
                    c.setState(ComponentState.SELECTED);
                    active = i;
                }
            }
        }
        Component de = dialog.components.get(active);

        if (de instanceof ComponentTextfield) {
            ((ComponentTextfield) de).append(RPG.keylistener.getAndClearCache());
            if (RPG.keylistener.isKeyDown2(KeyEvent.VK_BACK_SPACE)) {
                ((ComponentTextfield) de).backspace();
            }
        } else {
            RPG.keylistener.clearCache();
        }

        return false;
    }
}
